package entities;

import java.awt.Image;

import javax.swing.ImageIcon;

import constants.BrickColor;

import logic.Utilities;

/** The Brick's class for the brick breaker game.
 * @author Team 1
 *
 */
@SuppressWarnings("serial")
public class Brick extends Entity {

	private ImageIcon icon;		//The ImageIcon of the label.
	private BrickColor color;	//The color of the brick.
	private int lives;			//The remaining lives.
	
	/** Brick's constructor.
	 * @param x : the entity's upper-left corner's x coordinate.
	 * @param y : the entity's upper-left corner's y coordinate.
	 * @param color : the color of the brick.
	 * @param lives : the remaining lives of the brick.
	 * @param xScale : the x resize scale. If it's lower than one it reduces the images original width, increase it otherwise.
	 * @param yScale : the y resize scale. If it's lower than one it reduces the images original, increase it otherwise.
	 */
	public Brick(int x, int y, BrickColor color, int lives, float xScale, float yScale) {
		super(x, y);
		this.lives = lives;
		this.color = color;
		Image image;
		if(lives == 1) {
			image = Utilities.loadImage("res/brickRED.png");
			this.color = BrickColor.RED;
		} else {
			image = Utilities.loadImage("res/brick" + color.toString() + ".png");
		}
		if(xScale != 1 || yScale != 1) {
			image = Utilities.resizeImage(xScale, yScale, "res/brick" + this.color.toString() + ".png");
		}
		icon = new ImageIcon(image);
		this.setIcon(icon);
		this.width = image.getWidth(null);
		this.height = image.getHeight(null);
		this.setBounds((int)this.x, (int)this.y, this.width, this.height);
	}
	
	/** Gets brick's life number
	 * @return lives : life number.
	 */
	public int getLives() {
		return lives;
	}
	
	/** Set Brick's life number, if life number equals to one sets brick color to red.
	 * @param lifes : life number.
	 */
	public void setLifes(int lives) {
		this.lives = lives;
		if(lives == 1) {
			Image image = Utilities.loadImage("res/brickRED.png");
			float xScale = ((float)this.width) / image.getWidth(null);
			float yScale = ((float) this.height) / image.getHeight(null);
			if(xScale != 1 || yScale != 1) {
				image = Utilities.resizeImage(xScale, yScale, "res/brickRED.png");
			}
			this.color = BrickColor.RED;
			icon = new ImageIcon(image);
			setIcon(icon);
		}
	}
	
	/** Sets the color of the brick and changes the image for the label.
	 * @param color : the color of the brick.
	 */
	public void setColor(BrickColor color) {
		if(!this.color.equals(color)) {
			this.color = color;
			Image image = Utilities.loadImage("res/brick" + color.toString() + "png");
			float xScale = ((float)this.width) / image.getWidth(null);
			float yScale = ((float) this.height) / image.getHeight(null);
			if(xScale != 1 || yScale != 1) {
				image = Utilities.resizeImage(xScale, yScale, "res/brickRED.png");
			}
			this.color = BrickColor.RED;
			icon = new ImageIcon(image);
			setIcon(icon);
		}
	}
	
	public void resize(float xScale, float yScale) {
		super.resize(xScale, yScale);
		this.icon = new ImageIcon(Utilities.resizeImage(xScale, yScale, "res/brick" + color.toString() + ".png"));
		this.setIcon(icon);
	}
	
	/** Returns the actual color of the brick.
	 * @return color.
	 */
	public BrickColor getColor() {
		return color;
	}
	
	/** Return the brick's width for all resolution giving the windows width divided by 1280.
	 * @param xScale : The windows horizontal proportion value.
	 * @return The value of the ball's width depending on the horizontal scale.
	 */
	public static int getWidth(float xScale) {
		return Math.round(45 * xScale);
	}
	
	/**Return the brick's height for all resolution giving the windows height divided by 720.
	 * @param yScale : The windows vertical proportion value.
	 * @return The value of the ball's height depending on the vertical scale.
	 */
	public static int getHeight(float yScale) {
		return Math.round(22 * yScale);
	}
}
